package com.spider.silence.worker;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.spider.silence.downloader.WyzxDownloader;
import com.spider.silence.output.FileWriter;
import com.spider.silence.output.FileWriterPool;
import com.spider.silence.output.MongoWriter;
import com.spider.silence.output.MongoWriterPool;
import com.spider.silence.pool.WyzxDownloaderPool;
import com.spider.silence.task.WyzxTask;
import com.spider.silence.task.manager.CommonTaskManager;
import com.spider.silence.utils.DateUtils;
import com.spider.silence.utils.MD5Utils;
import org.apache.log4j.Logger;

import java.io.File;
import java.util.Map;

/**
 * @author Administrator
 * @description 描述
 * @time 2017/4/19 0019
 */
public class WyzxWorker implements  Runnable {
    private Logger logger = Logger.getLogger(WyzxWorker.class);
    @Override
    public void run() {
        while (true&&!Thread.interrupted()) {
            WyzxDownloaderPool pool = WyzxDownloaderPool.getInstance();
            WyzxDownloader downloader = null;
            FileWriter writer = null;
            FileWriterPool writerPool =FileWriterPool.getInstance();
            try {
                downloader = pool.fetchDownLoader(1000);
                WyzxTask task = (WyzxTask) CommonTaskManager.takeTask("Wyzx", 1000);
                if (downloader != null && task != null) {
                    JSONObject content = downloader.crawlItem(task.getUrl(), task.getChannel(), task.getClassId());
                    JSONObject data = content.getJSONObject("data");
                    if (data != null && !"".equals(data) && !data.isEmpty()&&data.get("publish_time")!=null&&!"".equals(data.get("publish_time"))) {
                        String filePrefix = "Wyzx";//文件前缀
                        long tsp = DateUtils.dateToTimestamp(DateUtils.parseDateTime(data.getString("publish_time"))).getTime();
                        String url = data.get("url").toString();
                        url = url.replaceAll("#.*", "");
                        String md5_url = MD5Utils.getHash3(url, "MD5");
                        //Writer writer =new FTPWriter();
                        String fileName = filePrefix + "_" + tsp + "_" + md5_url;//平台名称（或者英文）_ 新闻发布的时间戳 _ urlMD5码
                        String publishDate = DateUtils.formatDate(DateUtils.parseDate(data.getString("publish_time"))).replace("-", "-");
                        writer = writerPool.fetchWriter();
                        Map<String,File> map=downloader.getFiles();
                        writer.write("乌有之乡_speeches_" + publishDate, fileName, JSON.toJSONString(content, SerializerFeature.BrowserCompatible),map);

                    }
                }

            } catch (InterruptedException e) {
                logger.error(e.getMessage(), e);
                e.printStackTrace();
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
                e.printStackTrace();
            } finally {
                if (downloader != null) {
                    pool.releaseDownLoader(downloader);
                }
                if (writer != null) {
                    writerPool.releaseWriter(writer);
                }
            }
        }

    }
}
